package com.itheima.wemedia.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.PageRequestDto;
import com.itheima.common.pojo.Result;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.wemedia.dto.WmNewsDto;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
* <p>
* 自媒体图文内容信息表 控制器</p>
* @author czh
* @since 2021-08-20
*/
@RestController
@RequestMapping("/wmNews")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    //注入
    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }



    /**
     * 发布文章,如果有id那就是修改，没有的话就是添加
     * @return
     */
    /* @PostMapping("/save")
    public Result insert(@RequestBody WmNewsDtoSave wmNewsDtoSave){
        wmNewsService.insert(wmNewsDtoSave);


        return Result.ok();
    }*/
    @PostMapping("/save")
    public Result<WmNewsDtoSave> insert(@RequestBody  WmNewsDtoSave dtoSave) {

        wmNewsService.insert(dtoSave);

        return Result.ok();
    }




    /**
     * 通过id查询
     * @param id
     * @return
     */

    @GetMapping(value = "/one/{id}")
    public Result<WmNewsDtoSave> findById(@PathVariable Integer id) {

        WmNewsDtoSave wmNewsDtoSave = wmNewsService.findById(id);

        return Result.ok(wmNewsDtoSave);
    }

    /**
     * 文章删除
     */
    @Override
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Serializable id){
        int count = wmNewsService.deleteById(id);
        if (count>0){
            return Result.ok();
        }

        return Result.errorMessage("系统繁忙，请稍后再试");
    }


    /**
     * 分页查询
     */
    @PostMapping("/searchPage")
    public Result<PageInfo<WmNews>> pageList(@RequestBody PageRequestDto<WmNewsDto> dto){
        //创建分页对象
        Page<WmNews> wmNewsPage = new Page<>(dto.getPage(),dto.getSize());
        //查询条件封装
        WmNewsDto wmNewsDto = dto.getBody();
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<>();
        //执行分页查询
        if (wmNewsDto!=null){
            //状态查询
            if (!StringUtils.isEmpty(wmNewsDto.getStatus())){
                queryWrapper.eq("status",wmNewsDto.getStatus());
            }
            //关键词查询
            if (!StringUtils.isEmpty(wmNewsDto.getTitle())){
                queryWrapper.like("title",wmNewsDto.getTitle());
            }

            //频道查询
            if (!StringUtils.isEmpty(wmNewsDto.getChannelId())){
                queryWrapper.eq("channel_id",wmNewsDto.getChannelId());
            }

            //发布日期查询
            if (!StringUtils.isEmpty(wmNewsDto.getStartTime()) && !StringUtils.isEmpty(wmNewsDto.getEndTime())){
                queryWrapper.between("created_time",wmNewsDto.getStartTime(),wmNewsDto.getEndTime());
            }

        }
        //执行分页
        IPage<WmNews> wmNewsIPage = wmNewsService.page(wmNewsPage,queryWrapper);
        //构造分页结果
        PageInfo<WmNews> page = new PageInfo<>(dto.getPage(), dto.getSize(), wmNewsIPage.getTotal(), wmNewsIPage.getPages(), wmNewsIPage.getRecords());

        return Result.ok(page);
    }


    /**
     * 文章上架下架
     * @param id   文章id
     * @param enableId   1上架，  0 下架
     * @return
     */
    @PutMapping("/upOrDown/{id}/{enableId}")
    public Result updateUpDown(@PathVariable(value = "id")Integer id,@PathVariable(value = "enableId")Integer enableId){
        //查询文章是否存在
        WmNews wmNews = wmNewsService.getById(id);
        if (StringUtils.isEmpty(wmNews)){
            return Result.errorMessage("文章不存在");
        }
        Integer status = wmNews.getStatus();
        if (status != 9){
            return Result.errorMessage("文章未发布");
        }
        if (enableId>1 || enableId<0){
            return Result.errorMessage("请求参数  1上架，  0 下架");
        }
        wmNews.setEnable(enableId);
        wmNewsService.updateById(wmNews);
        return Result.ok();
    }



}